import {
  PanelItemController,
  ViewLayoutPanelController,
} from '@ibiz-template/runtime';
import { IPanelField, IAppIndexView } from '@ibiz/model-core';
import { PanelAppTitleState } from './panel-app-title.state';

/**
 * 面板应用标题控制器
 *
 * @export
 * @class PanelAppTitleController
 * @extends {PanelItemController<IPanelField>}
 */
export class PanelAppTitleController extends PanelItemController<IPanelField> {
  declare state: PanelAppTitleState;

  protected createState(): PanelAppTitleState {
    return new PanelAppTitleState(this.parent?.state);
  }

  /**
   * 面板控制器
   *
   * @type {ViewLayoutPanelController}
   * @memberof PanelAppTitleController
   */
  declare panel: ViewLayoutPanelController;

  /**
   * 分隔符，用于分割收缩与未收缩的标题
   * @return {*}
   * @author: zhujiamin
   * @Date: 2023-10-18 18:24:06
   */
  private captionSplit = '|';

  /**
   * 初始化
   *
   * @return {*}  {Promise<void>}
   * @memberof PanelAppTitleController
   */
  async onInit(): Promise<void> {
    await super.onInit();
    const indexViewModel: IAppIndexView = this.panel.view.model;
    // 抬头
    if (indexViewModel.title) {
      document.title = indexViewModel.title;
    }
    if (this.model.sysImage && this.model.sysImage.rawContent) {
      this.state.icon = this.model.sysImage.rawContent;
    } else if (indexViewModel.appIconPath) {
      // 图标路径
      this.state.icon = indexViewModel.appIconPath;
    }

    // 图标路径2(收缩时)
    if (indexViewModel.appIconPath2) {
      this.state.icon2 = indexViewModel.appIconPath2;
    }

    // 标题
    if (indexViewModel.caption) {
      this.state.caption = indexViewModel.caption.split(this.captionSplit)[0];
      this.state.caption2 =
        indexViewModel.caption.split(this.captionSplit)[1] ||
        indexViewModel.caption.split(this.captionSplit)[0];
    }

    // 子标题
    if (indexViewModel.subCaption) {
      this.state.subCaption = indexViewModel.subCaption.split(
        this.captionSplit,
      )[0];
      this.state.subCaption2 =
        indexViewModel.subCaption.split(this.captionSplit)[1] ||
        indexViewModel.subCaption.split(this.captionSplit)[0];
    }

    // 是否为 svg 图标
    if (this.state.icon.endsWith('.svg') || this.state.icon2.endsWith('.svg')) {
      this.state.isSvg = true;
    }
  }
}
